package com.example;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class WebMetaScraper {

    public static Map<MetadataType, String> scrapeMetadata(String url) {
        Map<MetadataType, String> metadata = new HashMap<>();
        try {
            Document doc = Jsoup.connect(url)
                    .timeout(10000)
                    .userAgent("Mozilla/5.0 (兼容爬虫)")
                    .get();

            // 提取所有需要的元数据
            for (MetadataType type : MetadataType.values()) {
                Element metaTag = doc.selectFirst("meta[name=" + type.getMetaName() + "]");
                String content = (metaTag != null) ? metaTag.attr("content") : "Not Found";
                metadata.put(type, content);
            }
            return metadata;

        } catch (IOException e) {
            System.err.println("访问链接失败: " + url);
            return new HashMap<>(); // 返回空 Map 表示错误
        }
    }
}